package com.easybuy.controller;

import com.easybuy.Tools.MemcachedJava;
import com.easybuy.Tools.Wares;
import com.easybuy.pojo.Product;
import com.easybuy.pojo.User;
import com.easybuy.service.ProductService.ProductService;
import com.easybuy.service.UserService.UserService;
import net.spy.memcached.internal.OperationFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
/*
* 用户登录验证
* 购物车，添加、修改、删除
* 收藏夹，添加、删除*/
public class UserController {
    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;

    @RequestMapping("/isUser")
    @ResponseBody
    /*用户验证*/
    public User user(HttpSession session){
        if (session.getAttribute("user")!=null){
            User user=(User)session.getAttribute("user");
            return userService.getUser(user.getId());
        }else {
            return null;
        }
    }


    @RequestMapping("/shopping")
    @ResponseBody
    /*购物车数据以、购物车生成*/
    public List shopping(@RequestParam String userId){
        List<Wares> shopList=null;
        Map map=null;
        System.out.println("shopping-userId:"+userId);
        if (MemcachedJava.getMcc().get(userId)==null){
            map=new HashMap();
            shopList=new ArrayList<Wares>();
            map.put("shopList",shopList);
            MemcachedJava.getMcc().set(userId,900, map);
            return null;
        }else {
            System.out.println("shopping-userId-status:true");
            map=(HashMap)MemcachedJava.getMcc().get(userId);
            shopList=(List<Wares>) map.get("shopList");
            return shopList;
        }

    }


    @RequestMapping("/addWares")
    @ResponseBody
    /*添加至购物车*/
    public boolean addWares(@RequestParam int id,@RequestParam String userId,@RequestParam int quantity){
        List<Wares> shopList=null;
        Map map=null;
        map=(HashMap)MemcachedJava.getMcc().get(userId);
        shopList=(ArrayList<Wares>)map.get("shopList");
        Product product=productService.selectProductById(id);
        Wares wares=new Wares();
        wares.setWares(product);
        wares.setQuantity(quantity);
        if (product.getStock()>=quantity){
            for (Wares wa:shopList){
                if (wa.getId()==wares.getId()){
                    wa.setQuantity(wa.getQuantity()+quantity);
                    if (wa.getQuantity()>wa.getStock()){
                        wa.setQuantity(wa.getStock());
                    }
                    MemcachedJava.getMcc().set(userId,900, map);
                    return true;
                }
            }
            shopList.add(wares);
            MemcachedJava.getMcc().set(userId,900, map);
            return true;
        }else return false;

    }


    @RequestMapping("/modifyWares")
    @ResponseBody
    /*修改购物车商品数*/
    public boolean modifyWares(@RequestParam Integer id,@RequestParam String userId,@RequestParam Integer quantity){
        List<Wares> shopList=null;
        Map map=null;
        map=(HashMap)MemcachedJava.getMcc().get(userId);
        shopList=(ArrayList<Wares>)map.get("shopList");
        Product product=productService.selectProductById(id);
        if (product.getStock()>=quantity){
            for (Wares wa:shopList){
                if (wa.getId()==id){
                    wa.setQuantity(quantity);
                    MemcachedJava.getMcc().set(userId,900, map);
                    return true;
                }
            }
        }
        MemcachedJava.getMcc().set(userId,900, map);
        return false;
    }


    @RequestMapping("/deleWares")
    @ResponseBody
    /*删除购物车商品*/
    public boolean deleWares(@RequestParam Integer id,@RequestParam String userId){
        Map map=(HashMap)MemcachedJava.getMcc().get(userId);
        List<Wares> shopList=(ArrayList<Wares>)map.get("shopList");
        for (Wares wa:shopList){
            if (wa.getId()==id){
                shopList.remove(wa);
                MemcachedJava.getMcc().set(userId,900, map);
                return true;
            }
        }
        MemcachedJava.getMcc().set(userId,900, map);
        return false;
    }


    @RequestMapping("/changeCollect")
    @ResponseBody
    /*收藏商品，取消收藏*/
    public boolean changeCollect(@RequestParam int id,@RequestParam String userId){
        List<Product> collect=null;
        Map map=null;
        map=(HashMap)MemcachedJava.getMcc().get(userId);
        if (map.get("collect")!=null){
            collect=(ArrayList<Product>)map.get("collect");
        }else {
            collect=new ArrayList<Product>();
            map.put("collect",collect);
        }
        Product product=productService.selectProductById(id);
        for (Product pr:collect){
            if (pr.getId()==product.getId()){
                collect.remove(pr);
                MemcachedJava.getMcc().set(userId,900, map);
                return false;
            }
        }
        collect.add(product);
        if (collect.size()>5)collect.remove(0);
        MemcachedJava.getMcc().set(userId,900, map);
        return true;

    }


    @RequestMapping("/getCollect")
    @ResponseBody
    /*收藏夹信息*/
    public List getCollect(@RequestParam String userId){
        List<Product> collect=null;
        Map map=null;
        map=(HashMap)MemcachedJava.getMcc().get(userId);
        collect=(List<Product>) map.get("collect");
        if (collect!=null){
            return collect;
        }else return null;
    }
}
